<?php
declare (strict_types=1);

namespace app\circle\logic\admin;

use app\admin\logic\system\Log;
use app\circle\model\admin\Comment as CommentModel;
use app\common\exception\LogicException;

/**
 * Class Comment
 *
 * @package app\circle\logic\admin
 */
class Comment extends CommentModel
{
    /**
     * 获取圈子的评论
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/16
     *
     * @param int $id
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getCommentsByCircleId(int $id)
    {
        return $this->with(['user', 'replyUserName'])->where('circle_id', $id)->append(['attach_url'])->paginate($this->getListRows());
    }

    /**
     * 删除评论
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/16
     *
     * @param int $id
     */
    public function deleteComments(int $id)
    {
        $comment = $this->findOrFail($id);

        if (!$comment->delete()) {
            throw new LogicException('删除失败');
        }
        // 减少评论数量
        app(Circle::class)->where('id', $comment->circle_id)->dec('comment_num')->update();
        // 操作日志
        Log::write('删除动态评论【' . $comment->content . '】', 'circle', 'delete', true);
    }
}